package org.jeecg.modules.business.third.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModelProperty;
import net.sf.json.JSONObject;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    //
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-23
 */
public class CommonUtil<T> {

    public static List<String> getTableNameList(String path) {
        List<String> list = new ArrayList<>();
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(path) + "/*.class";
        try {
            Resource[] resources = resolver.getResources(pattern);
            CachingMetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resolver);
            for (Resource resource : resources) {
                MetadataReader metadataReader = readerFactory.getMetadataReader(resource);
                // 扫描到的class
                String className = metadataReader.getClassMetadata().getClassName();
                Class<?> clazz = Class.forName(className);
                Service annotation = clazz.getAnnotation(Service.class);
                if (annotation != null && StrUtil.isNotBlank(annotation.value())) {
                    list.add(annotation.value());
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据注解校验数字
     *
     * @param t
     * @param fields
     */
    public static void validParamsNum(List<Object> ts, String... fields) {
        ts.stream().forEach(t -> {
            JSONObject jsonObject = JSONObject.fromObject(t);
            Field[] declaredFields = t.getClass().getDeclaredFields();
            Arrays.stream(declaredFields).forEach(field -> field.setAccessible(true));
            Arrays.stream(fields).forEach(goal -> {
                Optional<Field> first = Arrays.stream(declaredFields).filter(declaredField -> declaredField.getName().equals(goal)).findFirst();
                if (first.isPresent()) {
                    Field field = first.get();
                    if (ObjectUtil.isNotEmpty(jsonObject.get(goal))) {
                        ApiModelProperty declaredAnnotation = field.getDeclaredAnnotation(ApiModelProperty.class);
                        validParamNum(new BigDecimal(String.valueOf(jsonObject.get(goal))), declaredAnnotation.value());
                    }
                }
            });
        });
    }

    /**
     * 校验参数长度
     */
    public static void validParamNum(BigDecimal num, String msg) {
        if (ObjectUtil.isEmpty(num)) {
            return;
        }
        String numstr = num.toPlainString();
        String[] split = numstr.split("\\.");
        if (split.length <= 2) {
            if (split[0].length() > 8) {
                throw new JeecgBootException(msg + ":小数点前不能超过8位");
            }
            if (split.length > 1) {
                if (split[1].length() > 2) {
                    throw new JeecgBootException(msg + ":小数点后不能超过2位");
                }
            }
        }

    }

    public static void main(String[] args) {
//        validParamNum(null, "计划数量");
        WmsInReceiptTaskMaterial wmsInReceiptTaskMaterial = new WmsInReceiptTaskMaterial();
        wmsInReceiptTaskMaterial.setPlanQuantity(new BigDecimal("0.11111111"));
        validParamsNum(Arrays.asList(wmsInReceiptTaskMaterial), "planQuantity");
    }


}



